package com.bxd.baodan.adapter;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bxd.baodan.R;

public class ListDialogWithIconAdpater extends BaseAdapter {
	private int[] iconsResources;
	private String[] titles;
	private LayoutInflater inflater;
	private int textAligin = 0;//1,居左,2,居右,0居中
	public ListDialogWithIconAdpater(int[] iconsResources, String[] titles,
			Context context,int textAligin) {
		super();
		this.iconsResources = iconsResources;
		this.titles = titles;
		this.textAligin = textAligin;
		inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return titles.length;
	}

	@Override
	public Object getItem(int arg0) {
		return arg0;
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int ponsition, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.dialog_list_item, null);
			holder = new ViewHolder();
			holder.image = (ImageView) convertView
					.findViewById(R.id.dialog_item_img);
			holder.text = (TextView) convertView
					.findViewById(R.id.dialog_item_text);
			switch (textAligin) {
			case 0:
				break;
			case 1:
				holder.text.setGravity(Gravity.CENTER);
				break;
			case 2:
				holder.text.setGravity(Gravity.RIGHT);
				break;
			default:
				break;
			}
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if(iconsResources == null){
			holder.image.setVisibility(View.GONE);
		}else{
			holder.image.setVisibility(View.VISIBLE);
			holder.image.setImageResource(iconsResources[ponsition]);
		}
		holder.text.setText(titles[ponsition]);
		return convertView;
	}

	class ViewHolder {
		private TextView text;
		private ImageView image;

		public TextView getText() {
			return text;
		}

		public void setText(TextView text) {
			this.text = text;
		}

		public ImageView getImage() {
			return image;
		}

		public void setImage(ImageView image) {
			this.image = image;
		}

	}
}
